/*
 * Copyright 2008 Slava Chernyak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.chernyak.DataAdapter.types;

import com.chernyak.DataAdapter.Common;

public abstract class PrimitiveTypeDecl extends TypeDecl {

	
	public PrimitiveTypeDecl(String name, boolean compare, int ln) {
		super(name, compare, ln);
	}
	
	@Override
	public void lowerEquals(StringBuilder sink) {
		sink.append(Common.tabGen(2) +
				"__da_test &= (" + this.name + " == __da_o." + this.name + ");\n");
	}
	
	public void lowerComparisonWCG(StringBuilder sink) {
		if (compare) {
			sink.append("__da_c_result = (__da_c_result == 0) ? (" + name + " < __da_o." + name + ")? -1 : (" +
					name + " == __da_o." + name + " ? 0 : 1) : __da_c_result;\n"); 
		}
	}
	
	public void lowerToString(StringBuilder sink) {
		lowerToString(sink, 0);
	}
	
	protected void lowerToString(StringBuilder sink, int arrayDim) {
		sink.append("__da_str += " + name + genArrayDeref(arrayDim) + " + __DA_STRING_DELIM;");
	}
	
	public void lowerReadWCG(StringBuilder sink) {
		lowerRead(sink, 0);
	}
	
	protected void lowerRead(StringBuilder sink, int arrayDim) {
		sink.append(name + genArrayDeref(arrayDim) + " = ");
	}

	public void lowerWriteWCG(StringBuilder sink) {
		lowerWrite(sink, 0);
	}
	
	protected void lowerWrite(StringBuilder sink, int arrayDim) {
		sink.append(name + genArrayDeref(arrayDim));
	}
	
	public void lowerParse(StringBuilder sink) {
		lowerParse(sink, 0);
	}
	
	protected void lowerParse(StringBuilder sink,int arrayDim) {
		sink.append("__da_o." + name + genArrayDeref(arrayDim) + " = ");
	}
	
	protected abstract String getBoxedTypeName();
	protected abstract String getTypeName();
}

